﻿Imports System.Windows.Forms
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports Microsoft.Win32


Public Class Build_Type

    Public Sub cmb_Select_Import_Product_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_BuildType.Click
        cmb_BuildType.DroppedDown = True
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If cmb_BuildType.Text <> "" Then
            'Check if entry exists on table already
            Try
                Dim SQLResult As Object
                Dim Records_Retrieved As String = 0

                ESBD_Form.OpenSQLConnection()
                Dim connection As New SqlConnection(csb.ToString())
                cmd.Connection = connection
                connection.Open()
                cmd.CommandText = "Select Count(*) from BuildType_TB where Type = '" + cmb_BuildType.Text + "'"
                SQLResult = cmd.ExecuteScalar
                Records_Retrieved = SQLResult

                If Records_Retrieved = 0 Then
                    connection.Close()
                    Dim decision As DialogResult

                    decision = MsgBox(cmb_BuildType.Text + " does not exist in the database." + vbCrLf + vbCrLf + vbCrLf + "Add " + cmb_BuildType.Text + " to the database?", MsgBoxStyle.Question Or MsgBoxStyle.OkCancel)

                    If decision = DialogResult.OK Then
                        'Ask for password
                        If Admin_PW_Entered = "Y" Then
                            GoTo PW_Accepted
                        Else
                            Dim Passwd As String
                            Passwd = InputBox("Please enter Administrator password.", "Add Items Password")
                            If Passwd = Admin_PW Then
                                Admin_PW_Entered = "Y"
                                EditRec_PW_Entered = "Y"
                                DelRec_PW_Entered = "Y"
                                GoTo PW_Accepted
                            ElseIf Passwd = "" Then
                                GoTo End_Block
                            Else
                                MsgBox("Sorry the password you entered did not match the password on file." + vbCrLf + "Please check spelling and capitalization and try again.", MsgBoxStyle.Information)
                                GoTo End_Block
                            End If
                        End If

PW_Accepted:
                        Try
                            ESBD_Form.OpenSQLConnection()
                            cmd.Connection = connection
                            connection.Open()
                            cmd.CommandText = "INSERT INTO BuildType_TB ( [Type] ) VALUES('" + cmb_BuildType.Text + "');"
                            cmd.ExecuteScalar()
                            connection.Close()
                        Catch ex As Exception
                            connection.Close()
                            MsgBox(ex.Message)
                        End Try



                        If CB_Proto.Checked = True Then
                            If cmb_BuildType.Text = "" And TB_BuildTypeNumber.Text = "" Then
                                NewRecord_Form.TB_BuildType.Text = "Proto"
                            Else
                                NewRecord_Form.TB_BuildType.Text = cmb_BuildType.Text + TB_BuildTypeNumber.Text + " Proto"
                            End If
                        Else
                            NewRecord_Form.TB_BuildType.Text = cmb_BuildType.Text + TB_BuildTypeNumber.Text
                        End If

                        Me.DialogResult = System.Windows.Forms.DialogResult.OK

                        Me.Close()
End_Block:
                    Else 'Cancel or window closed
                        'Reload this window
                    End If
                Else
                    If CB_Proto.Checked = True Then
                        If cmb_BuildType.Text = "" And TB_BuildTypeNumber.Text = "" Then
                            NewRecord_Form.TB_BuildType.Text = "Proto"
                        Else
                            NewRecord_Form.TB_BuildType.Text = cmb_BuildType.Text + TB_BuildTypeNumber.Text + " Proto"
                        End If
                    Else
                        NewRecord_Form.TB_BuildType.Text = cmb_BuildType.Text + TB_BuildTypeNumber.Text
                    End If
                    Me.DialogResult = System.Windows.Forms.DialogResult.OK
                    connection.Close()
                End If
            Catch ex As Exception
                Dim connection As New SqlConnection(csb.ToString())
                connection.Close()
                MsgBox(ex.message)
            End Try
        Else
            If CB_Proto.Checked = True Then
                If cmb_BuildType.Text = "" And TB_BuildTypeNumber.Text = "" Then
                    NewRecord_Form.TB_BuildType.Text = "Proto"
                    Me.DialogResult = System.Windows.Forms.DialogResult.OK
                Else
                    NewRecord_Form.TB_BuildType.Text = cmb_BuildType.Text + TB_BuildTypeNumber.Text + " Proto"
                    Me.DialogResult = System.Windows.Forms.DialogResult.OK
                End If
            Else
                If TB_BuildTypeNumber.Text = "" Then
                    MsgBox("Build Type is required and cannot be blank." + vbCrLf + vbCrLf + "Please enter or select a Build Type.", MsgBoxStyle.Information)
                Else
                    NewRecord_Form.TB_BuildType.Text = cmb_BuildType.Text + TB_BuildTypeNumber.Text
                    Me.DialogResult = System.Windows.Forms.DialogResult.OK
                End If                
            End If
        End If
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Build_Type_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.cmb_BuildType.Select()
    End Sub

    Private Sub cmb_BuildType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_BuildType.SelectedIndexChanged
        'If cmb_BuildType.Text = "Proto" Then
        '    CB_Proto.Checked = False
        '    CB_Proto.Enabled = False
        'Else
        '    CB_Proto.Enabled = True
        'End If
    End Sub


    Private Sub BTN_Skip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Skip.Click

    End Sub

    Private Sub BTN_Previous_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Previous.Click
        BuildType_Prev = "Y"
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
End Class
